package com.jhws.business.car.service;

import com.alibaba.fastjson.JSON;
import com.jhws.business.car.bo.CarConstant;
import com.jhws.business.car.bo.CarMNSMessage;
import com.jhws.core.mns.bo.MNSMessage;
import com.jhws.core.mns.service.IMNSService;
import com.jhws.util.LogHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
@Service
public class MNSMessageService {
    @Resource
    private IMNSService mnsService;

    /**
     * 查询车道订单
     */
    public MNSMessage queryCarLaneOrder(String parkId, String laneCode) throws Exception {
        LogHelper.d("parkId: " + parkId + "       laneCode:" + laneCode);
        CarMNSMessage carMNSMessage = new CarMNSMessage();
        Map<String, Object> map = new HashMap<>();
        map.put("laneCode", laneCode);
        carMNSMessage.setType(CarConstant.CAR_MSG_TYPE_CAR_LANE_PAY);
        carMNSMessage.setContent(map);
        carMNSMessage.setTimeout(5);
        mnsService.producer(parkId, JSON.toJSONString(carMNSMessage));
        return getMNSMessage(parkId + "-" + laneCode);
    }

    /**
     * 查询车辆停车费
     */
    public MNSMessage queryCarFee(String parkId, String plateNumber, String tempQueue) throws Exception {
        LogHelper.d("parkId: " + parkId + "      plateNumber: " + plateNumber + "       tempQueue:" + tempQueue);
        CarMNSMessage carMNSMessage = new CarMNSMessage();
        Map<String, Object> map = new HashMap<>();
        map.put("plateNumber", plateNumber);
        carMNSMessage.setType(CarConstant.CAR_MSG_TYPE_QUERY_CAR_FEE);
        carMNSMessage.setContent(map);
        carMNSMessage.setTimeout(5);
        mnsService.producer(parkId, JSON.toJSONString(carMNSMessage));
        return getMNSMessage(tempQueue);
    }

    /**
     * 创建固定队列
     *
     * @param queueName
     * @param pollingWaitSeconds
     * @throws Exception
     */
    public void createQueue(String queueName, int pollingWaitSeconds) throws Exception {
        mnsService.createQueue(queueName, pollingWaitSeconds, 1296000L);
    }

    /**
     * 创建实时队列
     *
     * @param queueName
     * @param pollingWaitSeconds
     * @throws Exception
     */
    public void createQueue(String queueName, int pollingWaitSeconds, Long retentionPeriod) throws Exception {
        mnsService.createQueue(queueName, pollingWaitSeconds, retentionPeriod);
    }

    /**
     * 删除队列
     *
     * @param queueName
     * @throws Exception
     */
    public void deleteQueue(String queueName) throws Exception {
        mnsService.deleteQueue(queueName);
    }

    /**
     * 递归获取最新一次队列数据
     *
     * @param queueName
     * @return
     * @throws Exception
     */
    private MNSMessage getMNSMessage(String queueName) throws Exception {
        List<MNSMessage> mnsMessages = mnsService.comsumerList(queueName, 16);
        MNSMessage mnsMessage = null;
        if (mnsMessages != null && mnsMessages.size() > 0) {
            if (mnsMessages.size() == 1) {
                MNSMessage message = mnsMessages.get(0);
                if (message.getFirstDequeueTime() - message.getEnqueueTime() < 5000) {
                    return message;
                }
            }
            mnsMessage = getMNSMessage(queueName);
        }
        return mnsMessage;
    }
}
